import { message } from 'antd';
import api from '../../api';
export default {
  namespace: 'toolM',

  state: {
    queryBond: [], // 头部债券代码下拉列表
    basicWithIndicator: {}, // 工具箱--债券代码基本信息--债券价格试算弹窗数据
    priceTrial: {}, // 债券价格试算
  },

  effects: {
    // 查询债券代码下拉
    *queryBond({ payload }, { call, put }) {
      const response = yield call(api.queryBond, payload);
      if (response && response.success) {
        yield put({
          type: 'save',
          payload: {
            queryBond: response.data,
          },
        });
      } else {
        message.error('查询债券代码失败');
      }
    },

    // 查询债券代码基本信息--债券价格试算弹窗数据
    *basicInfo({ payload }, { call, put }) {
      const response = yield call(api.basicInfo, payload);
      if (response && response.success) {
        yield put({
          type: 'save',
          payload: {
            basicWithIndicator: response.data,
          },
        });
      } else {
        message.error('查询债券代码基本信息失败');
      }
    },

    // 债券价格试算
    *priceTrial({ payload, callback }, { call, put }) {
      const response = yield call(api.priceTrial, payload);
      if (response && response.success) {
        callback(response.data);
        yield put({
          type: 'save',
          payload: {
            priceTrial: response.data,
          },
        });
      } else {
        message.error('债券价格试算失败');
      }
    },
  },

  reducers: {
    save(state, { payload }) {
      return { ...state, ...payload };
    },
  },
};
